package com.lm;

import java.awt.Color;
import java.awt.Graphics2D;

/**
 *
 * @author Sajarwo Anggai
 */
public class Layer {

    private boolean show;
    private String name;
    private Tile tileContaner[][];
    private int blockTile[][];

    public Layer(String name, boolean show) {
        this.name = name;
        this.show = show;
    }

    public void initContainer(int rows, int cols) {
        tileContaner = new Tile[rows][cols];
        blockTile = new int[rows][cols];
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public boolean isShow() {
        return show;
    }

    public void setShow(boolean show) {
        this.show = show;
    }

    public Tile[][] getTileContaner() {
        return tileContaner;
    }

    public boolean isBlock(int value) {
        if (value == 0) {
            return false;
        } else {
            return true;
        }
    }

    public int[][] getBlockTile() {
        return blockTile;
    }

    public void draw(Graphics2D g2d, int pttX, int pttY, int pttW, int pttH, int tileW, int tileH) {
        int pos1 = 0, pos2;
        for (int i = pttX; i < pttW; i++) {
            pos2 = 0;
            for (int j = pttY; j < pttH; j++) {
                drawPosition(g2d, i, j, pos1, pos2, tileW, tileH);
                pos2++;
            }
            pos1++;
        }
    }

    public void drawPosition(Graphics2D g2d, int i, int j, int posx, int posy, int tileW, int tileH) {
        int tileX = WorkSpace.tilesToPixels(posx, tileW);
        int tileY = WorkSpace.tilesToPixels(posy, tileH);
        try {
            if (tileContaner[i][j] != null) {
                tileContaner[i][j].draw(g2d, tileX, tileY, tileW, tileH);
                if (tileContaner[i][j].getImageIdentity().isErased()) {
                    tileContaner[i][j] = null;
                }
            }
            if (isBlock(blockTile[i][j])) {
                g2d.setColor(Color.red);
                g2d.fillRect(tileX + 1, tileY + 1, tileW - 2, tileH - 2);
            }
        } catch (Exception e) {
            System.out.println("eror");
        }
    }
}
